package com.hcms.admin.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hcms.admin.service.ISysAppSettingService;
import com.hcms.common.api.CommonResponse;
import com.hcms.mybatis.entity.SysAppSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 应用表 前端控制器
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-03-29
 */
@RestController
@RequestMapping("/sysAppSetting")
public class SysAppSettingController {

    private static final long APP_SETTING_ID = 1;

    @Autowired
    ISysAppSettingService sysAppSettingService;

    @GetMapping("/setting")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysAppSetting>> setting() {
        LambdaQueryWrapper<SysAppSetting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysAppSetting::getId, APP_SETTING_ID);
        SysAppSetting setting = sysAppSettingService.getOne(queryWrapper);
        return CommonResponse.ok(setting);
    }

    @PostMapping("/setting")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> setting(@RequestBody SysAppSetting entity) {
        entity.setId(APP_SETTING_ID);
        entity.setCreateTime(new Date());
        sysAppSettingService.saveOrUpdate(entity);
        return CommonResponse.accepted("保存成功");
    }
}
